﻿using ExpressesClass.BestApi.Base;
using ExpressesClass.BestApi.kdTraceQueryReq;
using ExpressesClass.BestApi.kdTraceQueryRsp;
using ExpressesClass.BestApi.kdWaybillApplyNotifyReq;
using ExpressesClass.BestApi.kdWaybillApplyNotifyRsp;
using ExpressesClass.BestApi.kdWaybillDeliveryCancelNotifyReq;
using ExpressesClass.BestApi.kdWaybillDeliveryCancelNotifyRsp;
using ExpressesClass.BestApi.kdWaybillQueryNotifyReq;
using ExpressesClass.BestApi.kdWaybillQueryNotifyRsp;
using ExpressesClass.Model;
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace ExpressesClass.IExpresses
{
    public class BestExpress : IExpress
    {
        //百世快递参数
        private static string url = "http://ebill.ns.800best.com/kd/api/process";
        private static string partnerID = "";
        private static string partnerKey = "";
        private static string format = "JSON";
        private static string username = "";
        private static string password = "";
        //百世测试参数
        //private static string url = "http://openapi.800best.com/api-server/kd/api/process";
        //private static string partnerID = "";
        //private static string partnerKey = "";
        //private static string format = "JSON";
        //private static string username = "";
        //private static string password = "";
        public Kddh GetMailNo(Kddh kddh)
        {

            BestApi.kdWaybillApplyNotifyReq.EDIPrintDetailList ediPrintDetailList = new BestApi.kdWaybillApplyNotifyReq.EDIPrintDetailList();
            ediPrintDetailList.sendMan = kddh.sendMan;
            ediPrintDetailList.sendManPhone = kddh.sendManPhone;
            ediPrintDetailList.sendManAddress = kddh.sendManAddress;
            ediPrintDetailList.sendPostcode = kddh.sendPostcode;
            ediPrintDetailList.sendProvince = kddh.sendProvince;
            ediPrintDetailList.sendCity = kddh.sendCity;
            ediPrintDetailList.sendCounty = kddh.sendCounty;
            ediPrintDetailList.receiveMan = kddh.receiveMan;
            ediPrintDetailList.receiveManPhone = kddh.receiveManPhone;
            ediPrintDetailList.receiveManAddress = kddh.receiveManAddress;
            ediPrintDetailList.receivePostcode = kddh.receivePostcode;
            ediPrintDetailList.receiveProvince = kddh.receiveProvince;
            ediPrintDetailList.receiveCity = kddh.receiveCity;
            ediPrintDetailList.receiveCounty = kddh.receiveCounty;
            ediPrintDetailList.itemCount = int.Parse(kddh.itemCount);
            ediPrintDetailList.itemName = kddh.itemName;
            ediPrintDetailList.itemWeight = double.Parse(kddh.itemWeight);
            ediPrintDetailList.remark = kddh.remark;

            Client client = new Client(url, partnerKey, partnerID, format);

            KdWaybillApplyNotifyReq billPrintRequest = GetReq(ediPrintDetailList, kddh.shipName, kddh.shipVal, kddh.orderId);

            KdWaybillApplyNotifyRsp response = client.execute<KdWaybillApplyNotifyRsp>(billPrintRequest);

            if (response != null && response.result)
            {
                BestApi.kdWaybillApplyNotifyRsp.EDIPrintDetailList edi = response.EDIPrintDetailList[0];

                Kddh k = new Kddh();
                k.orderId = kddh.orderId;
                k.mailNo = edi.mailNo;
                k.markDestination = edi.markDestination;
                k.sortingCode = edi.sortingCode;
                k.pkgCode = edi.pkgCode;
                k.billProvideSiteName = edi.billProvideSiteName;
                k.billProvideSiteCode = edi.billProvideSiteCode;
                k.sendMan = edi.sendMan;
                k.sendManPhone = edi.sendManPhone;
                k.sendManAddress = edi.sendManAddress;
                k.sendPostcode = edi.sendPostcode;
                k.sendProvince = edi.sendProvince;
                k.sendCity = edi.sendCity;
                k.sendCounty = edi.sendCounty;
                k.sendCompany = kddh.sendCompany;
                k.receiveMan = edi.receiveMan;
                k.receiveManPhone = edi.receiveManPhone;
                k.receiveManAddress = edi.receiveManAddress;
                k.receivePostcode = edi.receivePostcode;
                k.receiveProvince = edi.receiveProvince;
                k.receiveCity = edi.receiveCity;
                k.receiveCounty = edi.receiveCounty;
                k.receiveCompany = kddh.receiveCompany;
                k.shipName = kddh.shipName;
                k.shipVal = kddh.shipVal;
                k.itemName = edi.itemName;
                k.itemWeight = edi.itemWeight.ToString();
                k.itemCount = edi.itemCount.ToString();
                k.remark = edi.remark;
                k.bm = kddh.bm;
                k.wzh = kddh.wzh;
                k.insuranceValue = "0";
                k.kdCompanys = kddh.kdCompanys;
                k.scenarios = kddh.scenarios;
                k.printNum = kddh.printNum;
                return k;
            }
            else if (response != null)
            {
                MessageBox.Show(response.errorDescription, "提示");
            }
            else
            {
                MessageBox.Show("获取快递电子面单失败.", "提示");
            }
            return null;
        }
        public bool CancelMailNo(Kddh kddh)
        {
            Client client = new Client(url, partnerKey, partnerID, format);

            KdWaybillDeliveryCancelNotifyReq kdWaybillDeliveryCancelNotify = new KdWaybillDeliveryCancelNotifyReq();
            List<RemovePrintFeedbackList> lists = new List<RemovePrintFeedbackList>();
            RemovePrintFeedbackList removePrintFeedbackList = new RemovePrintFeedbackList();
            removePrintFeedbackList.mailNo = kddh.mailNo;
            lists.Add(removePrintFeedbackList);
            kdWaybillDeliveryCancelNotify.removePrintFeedbackList = lists;

            BestApi.kdWaybillDeliveryCancelNotifyReq.Auth auth = new BestApi.kdWaybillDeliveryCancelNotifyReq.Auth();
            auth.username = username;
            auth.pass = password;
            kdWaybillDeliveryCancelNotify.auth = auth;

            KdWaybillDeliveryCancelNotifyRsp response = client.execute<KdWaybillDeliveryCancelNotifyRsp>(kdWaybillDeliveryCancelNotify);

            if (response != null && response.result)
            {
                return true;
            }
            else
            {
                MessageBox.Show("面单号:" + kddh.mailNo + " ,取消快递电子面单失败,请重试.", "提示");
            }
            return false;
        }
        public void GetUseNum()
        {
            Client client = new Client(url, partnerKey, partnerID, format);

            //创建请求对象
            KdWaybillQueryNotifyReq billCodeRequest = new KdWaybillQueryNotifyReq();
            billCodeRequest.printMan = "printMan";
            billCodeRequest.printSite = "printSite";

            BestApi.kdWaybillQueryNotifyReq.Auth auth = new BestApi.kdWaybillQueryNotifyReq.Auth();
            auth.username = username;
            auth.pass = password;
            billCodeRequest.auth = auth;

            //response即为百世的响应
            KdWaybillQueryNotifyRsp response = client.execute<KdWaybillQueryNotifyRsp>(billCodeRequest);

            if (response != null && response.result)
            {
                MessageBox.Show("可用量: " + response.count, "提示");
            }
            else if (response != null)
            {
                MessageBox.Show(response.errorDescription, "提示");
            }
            else
            {
                MessageBox.Show("查询失败", "提示");
            }
        }
        public List<TraceLog> GetExpressMsg(Kddh kddh)
        {

            Client client = new Client(url, partnerKey, partnerID, format);
            //创建请求对象
            KdTraceQueryReq kdTraceQuery = new KdTraceQueryReq();
            MailNos mailNos = new MailNos();
            List<String> list_mailNo = new List<String>();
            list_mailNo.Add(kddh.mailNo);
            mailNos.mailNo = list_mailNo;
            kdTraceQuery.mailNos = mailNos;

            //response即为百世的响应
            KdTraceQueryRsp response = client.execute<KdTraceQueryRsp>(kdTraceQuery);

            if (response != null && response.result)
            {
                if (response != null && response.traceLogs != null)
                {
                    List<Trace> traces = response.traceLogs[0].traces.trace;

                    List<TraceLog> log = new List<TraceLog>();
                    foreach (Trace t in traces)
                    {
                        log.Add(new TraceLog() { acceptTime = t.acceptTime, acceptAddress = t.acceptAddress, scanType = t.scanType, remark = t.remark });
                    }
                    return log;
                }
            }
            else if (response != null)
            {
                MessageBox.Show(response.errorDescription, "提示");
            }
            else
            {
                MessageBox.Show("查询失败", "提示");
            }
            return null;
        }
        private KdWaybillApplyNotifyReq GetReq(BestApi.kdWaybillApplyNotifyReq.EDIPrintDetailList ediPrintDetailList, string type, string money, string msgId)
        {
            KdWaybillApplyNotifyReq billPrintRequest = new KdWaybillApplyNotifyReq();
            billPrintRequest.deliveryConfirm = false;
            billPrintRequest.msgId = msgId;
            List<BestApi.kdWaybillApplyNotifyReq.EDIPrintDetailList> lists = new List<BestApi.kdWaybillApplyNotifyReq.EDIPrintDetailList>();

            if (type == "货到付款" || type == "代收货款")
            {
                ediPrintDetailList.shippingListAttribute = new List<BestApi.kdWaybillApplyNotifyReq.ShippingListAttribute>();
                BestApi.kdWaybillApplyNotifyReq.ShippingListAttribute ship = new BestApi.kdWaybillApplyNotifyReq.ShippingListAttribute();
                ship.shippingListAttributeValue = new List<BestApi.kdWaybillApplyNotifyReq.ShippingListAttributeValue>();
                ship.code = type == "货到付款" ? "BEST-FREIGHT" : "BEST-COD";
                BestApi.kdWaybillApplyNotifyReq.ShippingListAttributeValue shipVal = new BestApi.kdWaybillApplyNotifyReq.ShippingListAttributeValue();
                shipVal.name = "MONEY";
                shipVal.value = money;
                ship.shippingListAttributeValue.Add(shipVal);

                ediPrintDetailList.shippingListAttribute.Add(ship);
            }

            lists.Add(ediPrintDetailList);
            billPrintRequest.EDIPrintDetailList = lists;

            BestApi.kdWaybillApplyNotifyReq.Auth auth = new BestApi.kdWaybillApplyNotifyReq.Auth();
            auth.username = username;
            auth.pass = password;
            billPrintRequest.auth = auth;

            return billPrintRequest;
        }
    }
}
